﻿package view.component
{
    import __AS3__.vec.*;
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import morn.core.managers.*;
    import plugin.platform.*;
    import view.*;
    import view.component.activeBtns.*;
    import view.uilt.*;

    public class ActiveButton extends Sprite
    {
        public var _btnMsg:Object;
        public var btn:Button;
        private var eff:XMLMovieClip;
        private var btnEff:Class;
        public var _timebg:Image;
        public var _anglebg:Image;
        public var curtime:int = 0;
        public var _timetxt:Label;
        public var _angletxt:Label;
        private var _tipsTarget:String = "";
        private var _leafMsgArr:Array;
        private var _leafBtnDic:Dictionary;
        private var _leafShowState:Boolean = false;
        private var _curLeafShowVec:Vector.<ActiveButton>;
        private var _leafBg:Image;
        private var _isFirst:Boolean = true;
        private var showtime:Boolean = false;
        private var _cfgGuide:Object;
        private var objArr:Array;
        private var _cfgGuide1:Object;

        public function ActiveButton(param1:Object)
        {
            this.btnEff = ActiveButton_btnEff;
            this.objArr = [];
            this._cfgGuide1 = ConfigDictionary.data.newguide;
            if (param1)
            {
                this.data = param1;
            }
            return;
        }// end function

        public function set data(param1:Object) : void
        {
            var _loc_2:String = null;
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:Array = null;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            this._btnMsg = param1;
            if (this._btnMsg.name)
            {
                _loc_2 = "";
                if (this.btnMsg.name == "horse" && Config.hexie)
                {
                    _loc_2 = "png.activeButton." + "oldhorse";
                }
                else
                {
                    _loc_2 = "png.activeButton." + this.btnMsg.name;
                }
                if (!App.asset.hasClass(_loc_2))
                {
                    _loc_2 = "png.common.btn_newbuy";
                }
                if (this.btn)
                {
                    this.btn.skin = _loc_2;
                }
                else
                {
                    this.btn = new Button(_loc_2);
                    this.addChild(this.btn);
                    this.btn.x = this._btnMsg.sizeX * -35 * 0.8 + (this._btnMsg.sizeX > 1 ? (-5) : (0));
                    this.btn.y = this._btnMsg.sizeX * -35 * 0.8;
                    if (this._btnMsg.name == "bossChallenge")
                    {
                        this.btn.y = this.btn.y - 4;
                    }
                }
                this.btn.clickHandler = new Handler(this.doClick);
                if (this._cfgGuide == null)
                {
                    this._cfgGuide = ConfigDictionary.data.guideplugin;
                }
                if (this._cfgGuide["activemain"])
                {
                    for each (_loc_3 in this._cfgGuide["activemain"])
                    {
                        
                        if (this.btnMsg.name == _loc_3.eventparam)
                        {
                            this.btn.gname = _loc_3.eventparam;
                            ModelProxy.change("guideitem", "guideid" + _loc_3.id, this.btn);
                            this.btn.addEventListener(MouseEvent.CLICK, this.clickHandler);
                            if (!this.objArr[_loc_3.eventparam])
                            {
                                this.objArr[_loc_3.eventparam] = new Array();
                            }
                            this.objArr[_loc_3.eventparam][_loc_3.id] = _loc_3.id;
                        }
                    }
                }
            }
            else if (this.btn)
            {
                this.removeChild(this.btn);
                this.btn = null;
            }
            if (this._btnMsg.effect)
            {
                if (this.eff)
                {
                    this.removeChild(this.eff);
                    this.eff.dispose();
                    this.eff = null;
                }
                if (this._btnMsg.name == "fashen")
                {
                    if (ModelProxy.getValue("mainui", "chuzhanHero") != 1)
                    {
                        App.mloader3.loadAssets([{url:Config.getUrl("assets/images/activeButton/eff" + this._btnMsg.effect + ".swf"), type:ResLoader.SWF, size:100}], new Handler(this.comp));
                    }
                }
                else if (this._btnMsg.name == "mrlc")
                {
                    _loc_4 = UserData.getOnlineData("daypay");
                    _loc_5 = UserData.getOnlineData("daypayreward");
                    _loc_6 = ConfigDictionary.data.daypayreward;
                    _loc_7 = ModelProxy.getValue("data", "openday");
                    if (_loc_7 > 5)
                    {
                        _loc_7 = 5;
                    }
                    else if (!_loc_7)
                    {
                        _loc_7 = 1;
                    }
                    _loc_8 = [];
                    _loc_9 = 0;
                    if (_loc_5)
                    {
                        for each (_loc_10 in _loc_5)
                        {
                            
                            if (_loc_10 == 1)
                            {
                                _loc_8.push(_loc_10);
                            }
                        }
                        if (_loc_8.length == 5)
                        {
                            return;
                        }
                        _loc_9 = _loc_6[(_loc_8.length + 1) + "_" + _loc_7].money;
                    }
                    else
                    {
                        _loc_9 = _loc_6["1_" + _loc_7].money;
                    }
                    if (_loc_4 >= _loc_9)
                    {
                        App.mloader3.loadAssets([{url:Config.getUrl("assets/images/activeButton/eff" + this._btnMsg.effect + ".swf"), type:ResLoader.SWF, size:100}], new Handler(this.comp));
                    }
                }
                else
                {
                    App.mloader3.loadAssets([{url:Config.getUrl("assets/images/activeButton/eff" + this._btnMsg.effect + ".swf"), type:ResLoader.SWF, size:100}], new Handler(this.comp));
                }
            }
            switch(this._btnMsg.type)
            {
                case "1":
                {
                    this.createTime();
                    break;
                }
                case "2":
                {
                    this.createAngle();
                    break;
                }
                case "3":
                {
                    this.createTime();
                    this.createAngle();
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        private function clickHandler(event:Event) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = this.objArr[event.currentTarget.gname];
            for each (_loc_3 in _loc_2)
            {
                
                if (this._cfgGuide1[_loc_3].event == 6)
                {
                    if (GuideViewPao.getInstance().gid == _loc_3)
                    {
                        GuideViewPao.getInstance().hide();
                    }
                    continue;
                }
                ModelProxy.change("guide", "hide", _loc_3);
            }
            return;
        }// end function

        public function createTime(param1:int = 0) : void
        {
            if (!this._timetxt)
            {
                this._timetxt = new Label();
                this._timetxt.width = 68;
                this._timetxt.stroke = "0x0";
                this._timetxt.color = "0xffffff";
                this._timetxt.align = "center";
                this._timetxt.y = 21;
                this._timetxt.x = -35;
                this.addChild(this._timetxt);
                if (param1 > 0)
                {
                    this._timetxt.text = TimeDataFormatUtil.getLeftTime(param1);
                }
                else
                {
                    this._timetxt.text = "23:59:59";
                }
            }
            return;
        }// end function

        public function removeTime() : void
        {
            if (this._timebg)
            {
                if (this._timebg.parent)
                {
                    this._timebg.parent.removeChild(this._timebg);
                }
                this._timebg = null;
            }
            if (this._timetxt)
            {
                if (this._timetxt.parent)
                {
                    this._timetxt.parent.removeChild(this._timetxt);
                }
                this._timetxt = null;
            }
            return;
        }// end function

        public function createAngle() : void
        {
            if (!this._anglebg)
            {
                this._anglebg = new Image("png.common.team_gth");
                this._anglebg.y = -35;
                this._anglebg.x = 10;
                this.addChild(this._anglebg);
            }
            this._isFirst = true;
            this._anglebg.visible = true;
            return;
        }// end function

        private function comp() : void
        {
            this.eff = new XMLMovieClip("png.eff." + this.btnMsg.effect, new XML(new this.btnEff()));
            this.eff.mouseChildren = false;
            this.eff.mouseEnabled = false;
            this.eff.blendMode = BlendMode.ADD;
            this.eff.play();
            this.eff.smoothing = true;
            var _loc_1:Number = 0.8;
            this.eff.scaleY = 0.8;
            this.eff.scaleX = _loc_1;
            this.eff.x = -52;
            this.eff.y = -51;
            this.addChild(this.eff);
            if (this.btnMsg.name == "kaifujingji" || this.btnMsg.name == "fcactivity" || this.btnMsg.name == "baifu")
            {
                this.eff.visible = false;
                this._isFirst = false;
            }
            return;
        }// end function

        protected function doClick() : void
        {
            var _loc_1:ActiveButton = null;
            var _loc_2:Object = null;
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:Object = null;
            var _loc_6:String = null;
            var _loc_7:int = 0;
            var _loc_8:Date = null;
            var _loc_9:int = 0;
            var _loc_10:Date = null;
            if (this._btnMsg.leaf)
            {
                GlobalLayer.instance.root.removeEventListener(MouseEvent.MOUSE_DOWN, this.doClose);
                if (this._leafBg == null)
                {
                    this._leafBg = new Image("png.common.s9_bg4");
                    this.addChild(this._leafBg);
                    this._leafBg.sizeGrid = "20,20,20,20";
                    var _loc_11:int = 70;
                    this._leafBg.height = 70;
                    this._leafBg.width = _loc_11;
                    var _loc_11:int = 0;
                    this._leafBg.scaleY = 0;
                    this._leafBg.scaleX = _loc_11;
                }
                if (this._leafShowState)
                {
                    this._leafShowState = false;
                    if (this._curLeafShowVec)
                    {
                        while (this._curLeafShowVec.length > 0)
                        {
                            
                            _loc_1 = this._curLeafShowVec.shift();
                            TweenLite.to(_loc_1, 0.5, {scaleX:0, scaleY:0, x:0, y:0});
                        }
                    }
                    TweenLite.to(this._leafBg, 0.5, {scaleY:0, y:0, x:0, scaleX:0});
                }
                else
                {
                    this._leafShowState = true;
                    GlobalLayer.instance.root.addEventListener(MouseEvent.MOUSE_DOWN, this.doClose);
                    this.addEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
                    if (this._leafMsgArr == null)
                    {
                        this._leafMsgArr = [];
                        _loc_3 = this._btnMsg.leaf.split("#");
                        _loc_4 = ConfigDictionary.data.activeButton;
                        for each (_loc_5 in _loc_4)
                        {
                            
                            if (_loc_5.isLeaf == 1)
                            {
                                for each (_loc_6 in _loc_3)
                                {
                                    
                                    if (_loc_6 == _loc_5.name)
                                    {
                                        this._leafMsgArr.push(_loc_5);
                                    }
                                }
                            }
                        }
                    }
                    this._leafMsgArr.sortOn("index", Array.NUMERIC);
                    for each (_loc_2 in this._leafMsgArr)
                    {
                        
                        if (ActiveButtonUtil.checkShow(_loc_2, 1))
                        {
                            if (this._leafBtnDic)
                            {
                                _loc_1 = this._leafBtnDic[_loc_2.name];
                                if (_loc_1)
                                {
                                    this.checkLeafPosition(_loc_2, _loc_1);
                                }
                                else
                                {
                                    App.mloader3.loadSWF(Config.getUrl("assets/images/activeButton/" + _loc_2.name + ".swf"), 1, new Handler(this.loadComp, [_loc_2]), null, null, false);
                                }
                                continue;
                            }
                            this._leafBtnDic = new Dictionary();
                            App.mloader3.loadSWF(Config.getUrl("assets/images/activeButton/" + _loc_2.name + ".swf"), 1, new Handler(this.loadComp, [_loc_2]), null, null, false);
                        }
                    }
                }
                return;
            }
            else if (this._btnMsg.name == "fashen")
            {
                ModelProxy.change("tips", "state", "close");
                this.deleteEffect();
            }
            else if (this._btnMsg.name == "laobing")
            {
                this.deleteEffect();
            }
            else
            {
                if (this._btnMsg.name == "360jiasuqiu")
                {
                    PlatformSender._1360SpeedUpBall();
                    return;
                }
                if (this._btnMsg.name == "lianzhiexp")
                {
                    this._isFirst = false;
                    _loc_7 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                    _loc_8 = new Date();
                    _loc_8.setTime(_loc_7 * 1000);
                    _loc_9 = _loc_8.getHours();
                    if (_loc_9 >= 20)
                    {
                        _loc_10 = new Date(_loc_8.fullYear, _loc_8.month, _loc_8.date, 23, 59, 59, 59);
                        this.doAngleVisible(true);
                    }
                    else
                    {
                        _loc_10 = new Date(_loc_8.fullYear, _loc_8.month, _loc_8.date, 19, 59, 59, 59);
                        this.doAngleVisible(false);
                    }
                }
            }
            if (this._btnMsg.showName)
            {
                ModelProxy.change("plugin", "change", {target:this._btnMsg.showName, point:this.localToGlobal(new Point()), data:this._btnMsg.data});
            }
            else
            {
                ModelProxy.change("plugin", "change", {target:this._btnMsg.name, point:this.localToGlobal(new Point()), data:this._btnMsg.data});
            }
            return;
        }// end function

        protected function doStop(event:MouseEvent) : void
        {
            event.stopPropagation();
            return;
        }// end function

        protected function doClose(event:MouseEvent) : void
        {
            this.doClick();
            return;
        }// end function

        private function loadComp(param1:Object, param2) : void
        {
            var _loc_3:ActiveButton = null;
            var _loc_4:Class = null;
            switch(param1.cls)
            {
                case "1":
                {
                    _loc_4 = ShouCangBtn;
                    break;
                }
                case "2":
                {
                    _loc_4 = JhzButton;
                    break;
                }
                default:
                {
                    _loc_4 = ActiveButton;
                    break;
                    break;
                }
            }
            _loc_3 = new _loc_4(param1);
            this.addChild(_loc_3);
            this._leafBtnDic[param1.name] = _loc_3;
            if (param1.tips)
            {
                _loc_3.toolTips = this.btnMsg.tips;
            }
            if (this._leafShowState)
            {
                this.checkLeafPosition(param1, _loc_3);
            }
            return;
        }// end function

        private function checkLeafPosition(param1:Object, param2:ActiveButton) : void
        {
            var _loc_6:Object = null;
            param2.scaleX = 0;
            param2.scaleY = 0;
            TweenLite.to(param2, 0.5, {y:70, scaleX:1, scaleY:1});
            if (this._curLeafShowVec == null)
            {
                this._curLeafShowVec = new Vector.<ActiveButton>;
            }
            this._curLeafShowVec.push(param2);
            var _loc_3:* = this._curLeafShowVec.length;
            var _loc_4:int = 0;
            var _loc_5:Array = [];
            for each (param2 in this._curLeafShowVec)
            {
                
                _loc_5.push({index:param2.btnMsg.index, ab:param2});
            }
            _loc_5.sortOn("index", Array.NUMERIC);
            for each (_loc_6 in _loc_5)
            {
                
                TweenLite.to(_loc_6.ab, 0.5, {x:-30 * (_loc_3 - 1) + _loc_4 * 60});
                _loc_4++;
            }
            TweenLite.to(this._leafBg, 0.5, {x:-30 * _loc_3, y:35, width:60 * _loc_3, height:70, scaleX:1, scaleY:1});
            return;
        }// end function

        public function get btnMsg() : Object
        {
            return this._btnMsg;
        }// end function

        public function set toolTips(param1:String) : void
        {
            var _loc_2:Array = null;
            if (this.btn)
            {
                if (param1)
                {
                    _loc_2 = param1.split("#");
                    switch(int(_loc_2[0]))
                    {
                        case 1:
                        {
                            this.btn.toolTip = _loc_2[1];
                            break;
                        }
                        case 2:
                        {
                            this._tipsTarget = _loc_2[1];
                            addEventListener(MouseEvent.ROLL_OVER, this.onRollMouse);
                            addEventListener(MouseEvent.ROLL_OUT, this.onRollMouse);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            return;
        }// end function

        protected function onRollMouse(event:MouseEvent) : void
        {
            if (event.type == MouseEvent.ROLL_OVER)
            {
                ModelProxy.change("tips", this._tipsTarget, {data:"", p:localToGlobal(new Point(mouseX, mouseY))});
            }
            else
            {
                ModelProxy.change("tips", "state", "close");
            }
            return;
        }// end function

        public function setTimeShow(param1:int) : void
        {
            this.curtime = param1;
            if (this.curtime <= 0)
            {
                this.removeTime();
            }
            else
            {
                this.createTime(param1);
            }
            this.showtime = true;
            return;
        }// end function

        public function doTime() : Boolean
        {
            if (this._btnMsg.type < 1)
            {
                return true;
            }
            var _loc_1:String = this;
            var _loc_2:* = this.curtime - 1;
            _loc_1.curtime = _loc_2;
            if (this.curtime <= -1)
            {
                if (this._btnMsg.name == "activity")
                {
                    return this.checkOnlineTime();
                }
                if (this.showtime)
                {
                    this.removeTime();
                    return true;
                }
                ModelProxy.change("mainui", "removeBtn", this._btnMsg.name);
                return true;
            }
            else
            {
                if (this.curtime > 86400)
                {
                    this._timetxt.text = TimeDataFormatUtil.getLeftTimeStr4(this.curtime, 2);
                    this._timetxt.color = MColor.T_WHITE;
                    return false;
                }
                if (this.curtime <= 3600)
                {
                    this._timetxt.text = TimeDataFormatUtil.getLeftTime2(this.curtime);
                    this._timetxt.color = MColor.T_RED;
                    return false;
                }
                this._timetxt.text = TimeDataFormatUtil.getLeftTime2(this.curtime);
                this._timetxt.color = MColor.T_WHITE;
            }
            return false;
        }// end function

        public function checkOnlineTime() : Boolean
        {
            var _loc_2:Number = NaN;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            if (this._btnMsg.type < 1)
            {
                return true;
            }
            var _loc_1:* = UserData.getOnlineData("curid");
            if (_loc_1 >= 5)
            {
                this.doTimeVisible(false);
                this.doAngleVisible(false);
                this.deleteEffect();
                return true;
            }
            _loc_2 = UserData.getOnlineData("lastupdate");
            _loc_3 = UserData.getOnlineData("ontime");
            _loc_4 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
            _loc_5 = ConfigDictionary.data.online;
            _loc_6 = _loc_4 - int(_loc_2 / 1000) + _loc_3;
            _loc_7 = 0;
            _loc_8 = _loc_1;
            while (_loc_8 < 5)
            {
                
                _loc_9 = int(_loc_5[_loc_8].time) * 60;
                if (_loc_6 < _loc_9)
                {
                    this.curtime = _loc_9 - _loc_6;
                    this._angletxt.text = _loc_7 + "";
                    this.doAngleVisible(true);
                    this.addEffect();
                    return false;
                }
                _loc_7++;
                _loc_8++;
            }
            this.doTimeVisible(false);
            this._angletxt.text = _loc_7 + "";
            return true;
        }// end function

        public function doTimeVisible(param1:Boolean) : void
        {
            if (this._timetxt != null)
            {
                this._timetxt.visible = param1;
            }
            return;
        }// end function

        public function doAngleVisible(param1:Boolean) : void
        {
            if (!this._isFirst)
            {
                if (this._anglebg != null)
                {
                    this._anglebg.visible = param1;
                }
                if (this._angletxt != null)
                {
                    this._angletxt.visible = param1;
                }
            }
            return;
        }// end function

        public function deleteEffect() : void
        {
            if (this.eff)
            {
                this.eff.visible = false;
            }
            return;
        }// end function

        public function addEffect() : void
        {
            if (this.eff != null)
            {
                this.eff.visible = true;
            }
            else
            {
                App.mloader3.loadAssets([{url:Config.getUrl("assets/images/activeButton/eff" + this._btnMsg.effect + ".swf"), type:ResLoader.SWF, size:100}], new Handler(this.comp));
            }
            return;
        }// end function

        public function disposeLeaf() : void
        {
            var _loc_1:String = null;
            var _loc_2:ActiveButton = null;
            GlobalLayer.instance.root.removeEventListener(MouseEvent.MOUSE_DOWN, this.doClose);
            this.removeEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            if (this._curLeafShowVec != null)
            {
                while (this._curLeafShowVec.length > 0)
                {
                    
                    this._curLeafShowVec.shift();
                }
            }
            for (_loc_1 in this._leafBtnDic)
            {
                
                _loc_2 = this._leafBtnDic[_loc_1];
                _loc_2.disposeLeaf();
                this.removeChild(_loc_2);
                _loc_2 = null;
                delete this._leafBtnDic[_loc_1];
            }
            this._leafShowState = false;
            return;
        }// end function

        public function removeLeaf(param1:String) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:ActiveButton = null;
            var _loc_2:* = this._leafBtnDic[param1];
            if (_loc_2)
            {
                this._curLeafShowVec.splice(this._curLeafShowVec.indexOf(_loc_2), 1);
                _loc_3 = this._curLeafShowVec.length;
                _loc_4 = 0;
                for each (_loc_5 in this._curLeafShowVec)
                {
                    
                    TweenLite.to(_loc_5, 0.5, {x:-30 * (_loc_3 - 1) + _loc_4 * 60});
                    _loc_4++;
                }
                TweenLite.to(this._leafBg, 0.5, {x:-30 * _loc_3, y:35, width:60 * _loc_3, height:70, scaleX:1, scaleY:1});
                _loc_2.disposeLeaf();
                this.removeChild(_loc_2);
                _loc_2 = null;
                delete this._leafBtnDic[param1];
            }
            return;
        }// end function

    }
}
